package com.cash.channel.svc.logic;

import com.cash.channel.api.util.ValidatorUtil;
import com.cash.channel.svc.model.OrderInfoModel;
import com.alibaba.fastjson.JSON;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.request.ApplySubmitRequest;
import com.cash.user.model.Account;
import com.cash.user.model.UserIdVerifiedModel;
import com.cash.user.model.UserInfoModel;
import com.cash.user.service.UserService;
import com.geex.haneda.model.api.api00001.UserQCDataModel;
import com.geex.haneda.model.api.api00004.BasicDataModel;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Slf4j
@Component
public class UserLogic {

    @Resource
    private UserService userService;

    @Resource
    private ThirdCommonLogic thirdCommonLogic;

    public Boolean registerChannelAccount(String mobile, String channel) {
        return userService.addThirdPartyAccount(mobile, channel);
    }

    public String saveUserInfoModel(UserInfoModel userInfoModel) throws CashChannelException {
        try {
            return userService.saveUserInfo(userInfoModel);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CashChannelException("生成用户信息失败");
        }
    }

    public void bindUUIDToAccount(String uuid, String mobile) {
        userService.bindUserIdToMobile(mobile, uuid);
    }

    public Account findAccountByUid(String uid) {
        return userService.findUserAccountByUid(uid);
    }

    public void insertUserIdVerified(OrderInfoModel orderInfoModel, ApplySubmitRequest applySubmitRequest, String frontPath, String backPath, String channel) throws CashChannelException {
        log.info("插入ocr认证信息");
        try {
            Account account = userService.findUserAccountByUid(orderInfoModel.getcUid());
            UserIdVerifiedModel userIdVerifiedModel = new UserIdVerifiedModel();
            userIdVerifiedModel.setCUid(orderInfoModel.getcUid());
            userIdVerifiedModel.setdCreate(new Date());
            userIdVerifiedModel.setAccountId(account.getId());
            userIdVerifiedModel.setVerifiedIdGender(getGenderByIdNo(orderInfoModel.getcIdno()));
            userIdVerifiedModel.setVerifiedIdAddress(applySubmitRequest.getIdInfo().getVerifiedIdAddress());
            userIdVerifiedModel.setVerifiedIdRace(applySubmitRequest.getIdInfo().getVerifiedIdRace());
            userIdVerifiedModel.setVerifiedIdNo(applySubmitRequest.getIdInfo().getVerifiedIdNo());
            userIdVerifiedModel.setVerifiedIdBirthday(applySubmitRequest.getIdInfo().getVerifiedIdBirthday());
            userIdVerifiedModel.setVerifiedIdFrontPath(frontPath);
            userIdVerifiedModel.setVerifiedIdBackPath(backPath);
            userIdVerifiedModel.setcCreator(channel);
            userIdVerifiedModel.setVerifiedIdName(applySubmitRequest.getIdInfo().getVerifiedIdName());
            userIdVerifiedModel.setVerifiedIdIssued(applySubmitRequest.getIdInfo().getVerifiedIdIssued());
            userIdVerifiedModel.setVerifiedIdValid(orderInfoModel.getcIdnoValid());
            userIdVerifiedModel.setVerifiedIdValidBegin(applySubmitRequest.getIdInfo().getVerifiedIdValidBegin());
            userIdVerifiedModel.setVerifiedIdValidEnd(applySubmitRequest.getIdInfo().getVerifiedIdValidEnd());
            userService.insertUserIdVerified(userIdVerifiedModel);
            log.info("插入ocr认证信息完成：" + JSON.toJSONString(userIdVerifiedModel));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new CashChannelException("插入ocr认证信息失败");
        }
    }

    public String getGenderByIdNo(String idNo) throws CashChannelException {
        if (!ValidatorUtil.checkIdNo(idNo)) {
            throw new CashChannelException("身份证参数异常:"+idNo);
        }
        Character character = idNo.charAt(16);
        Integer char17 = Integer.parseInt(character.toString());
        return char17%2==1?"男":"女";
    }

    public Map<String, Object> pushQcInfoToHaneda(OrderInfoModel orderInfoModel, String channel, String orderNo, String dataUrl) throws CashChannelException {
        UserQCDataModel userQCDataModel = thirdCommonLogic.createUserQcDataModelByOrderInfoModel(orderInfoModel, channel);
        BasicDataModel basicDataModel = thirdCommonLogic.createBasicDataModelByOrderInfoModel(orderInfoModel);
        Map<String, Object> do360IdentifiOrderAPIParams = Maps.newHashMap();
        do360IdentifiOrderAPIParams.put("UserQCDataModel", userQCDataModel);
        do360IdentifiOrderAPIParams.put("BasicDataModel", basicDataModel);
        do360IdentifiOrderAPIParams.put("terminalModel", getTerminalModel(orderInfoModel));
        do360IdentifiOrderAPIParams.put("cOutAppId", orderNo);
        do360IdentifiOrderAPIParams.put("nOutType", channel);
        do360IdentifiOrderAPIParams.put("cChannel", channel);
        do360IdentifiOrderAPIParams.put("report", dataUrl);
        do360IdentifiOrderAPIParams.put("uid", orderInfoModel.getcUid());
        Map<String, Object> apiResult = thirdCommonLogic.do360IdentifiOrderAPI(do360IdentifiOrderAPIParams);
        return apiResult;
    }

    private Map<String, Object> getTerminalModel(OrderInfoModel orderInfoModel) {
        Map<String, Object> resultMap = Maps.newHashMap();
        resultMap.put("cClientIp", orderInfoModel.getcClientIp());
        resultMap.put("cMacAddr", orderInfoModel.getcMacAddr());
        resultMap.put("cOsType", orderInfoModel.getcOsType());
        resultMap.put("cImei", orderInfoModel.getcImei());
        resultMap.put("cLongitude", orderInfoModel.getcLongitude());
        resultMap.put("cLatitude", orderInfoModel.getcLatitude());
        return resultMap;
    }

    public UserInfoModel findUserInfoModelByUid(String uid) throws CashChannelException {
        UserInfoModel userInfoModel = userService.findUserInfoByUid(uid);
        log.debug("获取用户信息："+ JSON.toJSONString(userInfoModel));
        if (userInfoModel == null || StringUtils.isEmpty(userInfoModel.getcName())
                || StringUtils.isEmpty(userInfoModel.getcIdno())) {
            throw new CashChannelException("未找到用户信息");
        }
        return userInfoModel;
    }
}
